package com.wzdigit.wms.raw.controller.inv;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.basic.annotation.FormLock;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.inv.CreateOrUpdateHoldHeaderRequest;
import com.wzdigit.wms.basic.client.request.inv.SearchHoldDtlListForWaitFreezeByAsnRequest;
import com.wzdigit.wms.basic.client.request.inv.SearchHoldDtlListForWaitFreezeByLotnoRequest;
import com.wzdigit.wms.basic.client.request.inv.SearchHoldDtlListForWaitFreezeByOqcRequest;
import com.wzdigit.wms.basic.client.request.inv.SearchHoldDtlListForWaitFreezeBySkuRequest;
import com.wzdigit.wms.basic.client.request.inv.SearchHoldDtlListRequest;
import com.wzdigit.wms.basic.client.request.inv.SearchHoldFreezeRequest;
import com.wzdigit.wms.basic.client.request.inv.SearchHoldHeaderRequest;
import com.wzdigit.wms.basic.client.request.inv.SearchInvHoldByAsnRequest;
import com.wzdigit.wms.basic.client.request.inv.SearchInvHoldByOqcRequest;
import com.wzdigit.wms.basic.client.request.inv.SearchInvHoldBySkuRequest;
import com.wzdigit.wms.basic.client.request.inv.SubmitHoldFreezeRequest;
import com.wzdigit.wms.basic.client.request.inv.SubmitHoldThawRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.inv.HoldDtlGoodsResponse;
import com.wzdigit.wms.basic.client.response.inv.HoldDtlListForWaitFreezeResponse;
import com.wzdigit.wms.basic.client.response.inv.HoldDtlListResponse;
import com.wzdigit.wms.basic.client.response.inv.HoldErrorGoodsResponse;
import com.wzdigit.wms.basic.client.response.inv.HoldHeaderResponse;
import com.wzdigit.wms.basic.client.response.inv.InvHoldByAsnResponse;
import com.wzdigit.wms.basic.client.response.inv.InvHoldByLontnoResponse;
import com.wzdigit.wms.basic.client.response.inv.InvHoldByOqcResponse;
import com.wzdigit.wms.basic.client.response.inv.InvHoldBySkuResponse;
import com.wzdigit.wms.common.utils.ValidatorUtil;
import com.wzdigit.wms.raw.constant.RawWareConstant;
import com.wzdigit.wms.raw.service.inv.HoldService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 库存冻结/解冻
 * @author Administrator
 *
 */
@RestController
@Api(tags = "库存冻结或解冻")
@RequestMapping(value = RawWareConstant.API_VERSION)
public class HoldController {
	
	public static final String REQUEST_MODEL = "/hold";

    @Autowired
    private HoldService holdService;

    @ApiOperation(value = "主档查询", notes = "主档查询")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/selectHoldHeaderList")
    public PagerInfo<List<HoldHeaderResponse>> selectHoldHeaderList(
    		SearchHoldHeaderRequest search, Pager<?> pager,
            HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
    	try {
            return holdService.selectHoldHeaderList(search, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "创建/修改主档",notes = "创建/修改主档")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/createOrUpdateHoldHeader")
    public Result<Integer> createOrUpdateHoldHeader(
    		@RequestBody @Valid CreateOrUpdateHoldHeaderRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return holdService.createOrUpdateHoldHeader(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "删除主档", notes = "删除主档")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @DeleteMapping(value = REQUEST_MODEL + "/deleteHoldHeader")
    public Result<?> deleteHoldHeader(
    		@ApiParam(value = "主档ID", required = true) 
    		@RequestParam(required = true) Integer holdHeaderId, 
    		HttpServletRequest servletRequest, 
    		HttpServletResponse servletResponse) {
    	try {
            return holdService.deleteHoldHeader(holdHeaderId, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "明细查询", notes = "明细查询")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/selectHoldDtlList")
    public PagerInfo<List<HoldDtlListResponse>> selectHoldDtlList(
    		SearchHoldDtlListRequest search, Pager<?> pager,
            HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
    	try {
            return holdService.selectHoldDtlList(search, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "序号明细查询", notes = "序号明细查询")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/selectHoldDtlGoodsList")
    public PagerInfo<List<HoldDtlGoodsResponse>> selectHoldDtlGoodsList(
    		@ApiParam(value = "明细ID", required = true) 
    		@RequestParam(required = true) Integer holdDtlId, 
    		@ApiParam(value = "条码SN", required = false) 
    		@RequestParam(required = false) String goodsSn, 
    		Pager<?> pager,
            HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
    	try {
            return holdService.selectHoldDtlGoodsList(holdDtlId, goodsSn, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @FormLock(isdeal = false)
    @ApiOperation(value = "待冻结明细==》序号明细查询", notes = "待冻结明细==》序号明细查询")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/selectHoldDtlGoodsListForWaitFreeze")
    public PagerInfo<List<HoldDtlGoodsResponse>> selectHoldDtlGoodsListForWaitFreeze(
    		@RequestBody SearchHoldFreezeRequest dto,
            HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
    	try {
            return holdService.selectHoldDtlGoodsListForWaitFreeze(dto);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "【送货单冻结物料选择】查询", notes = "【送货单冻结物料选择】查询")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/selectInvHoldByAsn")
    public PagerInfo<List<InvHoldByAsnResponse>> selectInvHoldByAsn(
    		SearchInvHoldByAsnRequest dto, Pager<?> pager,
    		HttpServletRequest servletRequest, 
    		HttpServletResponse servletResponse) {
    	try {
            return holdService.selectInvHoldByAsn(dto, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "【检验单冻结物料选择】查询", notes = "【检验单冻结物料选择】查询")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/selectInvHoldByOqc")
    public PagerInfo<List<InvHoldByOqcResponse>> selectInvHoldByOqc(
    		SearchInvHoldByOqcRequest dto, Pager<?> pager,
    		HttpServletRequest servletRequest, 
    		HttpServletResponse servletResponse) {
    	try {
            return holdService.selectInvHoldByOqc(dto, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "【料号冻结物料选择】查询", notes = "【料号冻结物料选择】查询")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/selectInvHoldBySku")
    public PagerInfo<List<InvHoldBySkuResponse>> selectInvHoldBySku(
    		SearchInvHoldBySkuRequest dto, Pager<?> pager,
    		HttpServletRequest servletRequest, 
    		HttpServletResponse servletResponse) {
    	try {
            return holdService.selectInvHoldBySku(dto, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "【批次冻结物料选择】查询", notes = "批次冻结物料选择】查询")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/selectInvHoldByLontno")
    public PagerInfo<List<InvHoldByLontnoResponse>> selectInvHoldByLontno(
    		@ApiParam(value = "批次号", required = false) 
    		@RequestParam(required = false) String lotno, 
    		Pager<?> pager,
    		HttpServletRequest servletRequest, 
    		HttpServletResponse servletResponse) {
    	try {
            return holdService.selectInvHoldByLontno(lotno, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @FormLock(isdeal = false)
    @ApiOperation(value = "【送货单冻结物料选择=》统计待冻结明细】查询", notes = "【送货单冻结物料选择=》统计待冻结明细】查询")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/selectHoldDtlListForWaitFreezeByAsn")
    public PagerInfo<List<HoldDtlListForWaitFreezeResponse>> selectHoldDtlListForWaitFreezeByAsn(
    		@RequestBody SearchHoldDtlListForWaitFreezeByAsnRequest dto, 
    		HttpServletRequest servletRequest, 
    		HttpServletResponse servletResponse) {
    	try {
            return holdService.selectHoldDtlListForWaitFreezeByAsn(dto);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @FormLock(isdeal = false)
    @ApiOperation(value = "【检验单冻结物料选择=》统计待冻结明细】查询", notes = "【检验单冻结物料选择=》统计待冻结明细】查询")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/selectHoldDtlListForWaitFreezeByOqc")
    public PagerInfo<List<HoldDtlListForWaitFreezeResponse>> selectHoldDtlListForWaitFreezeByOqc(
    		@RequestBody SearchHoldDtlListForWaitFreezeByOqcRequest dto, 
    		HttpServletRequest servletRequest, 
    		HttpServletResponse servletResponse) {
    	try {
            return holdService.selectHoldDtlListForWaitFreezeByOqc(dto);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @FormLock(isdeal = false)
    @ApiOperation(value = "【料号冻结物料选择=》统计待冻结明细】查询", notes = "【料号冻结物料选择=》统计待冻结明细】查询")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/selectHoldDtlListForWaitFreezeBySku")
    public PagerInfo<List<HoldDtlListForWaitFreezeResponse>> selectHoldDtlListForWaitFreezeBySku(
    		@RequestBody SearchHoldDtlListForWaitFreezeBySkuRequest dto, 
    		HttpServletRequest servletRequest, 
    		HttpServletResponse servletResponse) {
    	try {
            return holdService.selectHoldDtlListForWaitFreezeBySku(dto);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @FormLock(isdeal = false)
    @ApiOperation(value = "【批次号冻结物料选择=》统计待冻结明细】查询", notes = "【批次号冻结物料选择=》统计待冻结明细】查询")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/selectHoldDtlListForWaitFreezeByLontno")
    public PagerInfo<List<HoldDtlListForWaitFreezeResponse>> selectHoldDtlListForWaitFreezeByLontno(
    		@RequestBody SearchHoldDtlListForWaitFreezeByLotnoRequest dto, 
    		HttpServletRequest servletRequest, 
    		HttpServletResponse servletResponse) {
    	try {
            return holdService.selectHoldDtlListForWaitFreezeByLontno(dto);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    
    @ApiOperation(value = "冻结", notes = "冻结")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/submitHoldFreeze")
    public Result<String> submitHoldFreeze(
    		@RequestBody @Valid SubmitHoldFreezeRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
    		HttpServletResponse servletResponse) throws Exception {
    	try {
    		ValidatorUtil.validator(bindingResult);
            return holdService.submitHoldFreeze(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "解冻", notes = "解冻")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/submitHoldThaw")
    public Result<?> submitHoldThaw(
    		@RequestBody @Valid SubmitHoldThawRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
    		HttpServletResponse servletResponse) throws Exception {
    	try {
    		ValidatorUtil.validator(bindingResult);
            return holdService.submitHoldThaw(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "根据缓存key查找冻结失败的条码信息", notes = "根据缓存key查找冻结失败的条码信息")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/selectErrorHoldGoods")
    public PagerInfo<List<HoldErrorGoodsResponse>> selectErrorHoldGoods(
    		@ApiParam(value = "缓存key", required = true) 
    		@RequestParam(required = true) String errorHoldGoodsKey,
    		Pager<?> pager,
            HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
    	try {
            return holdService.selectErrorHoldGoods(errorHoldGoodsKey, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "根据缓存key导出冻结失败的条码信息", notes = "根据缓存key导出冻结失败的条码信息")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/exportErrorHoldGoods")
    public void exportErrorHoldGoods(
    		@ApiParam(value = "缓存key", required = true) 
    		@RequestParam(required = true) String errorHoldGoodsKey,
    		HttpServletRequest servletRequest, 
    		HttpServletResponse servletResponse) throws Exception {
    	try {
    		holdService.exportErrorHoldGoods(errorHoldGoodsKey, servletRequest, servletResponse);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "根据缓存key关闭冻结失败的条码信息", notes = "根据缓存key导出冻结失败的条码信息")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @DeleteMapping(value = REQUEST_MODEL + "/closeErrorHoldGoods")
    public Result<?> closeErrorHoldGoods(
    		@ApiParam(value = "缓存key", required = true) 
    		@RequestParam(required = true) String errorHoldGoodsKey,
    		HttpServletRequest servletRequest, 
    		HttpServletResponse servletResponse) throws Exception {
    	try {
    		return holdService.closeErrorHoldGoods(errorHoldGoodsKey);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
}